package com.chainyoung.trademarket.ui;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.appcommon.entity.Version;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.AppUtils;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.update.UpdateService;

import static com.chainyoung.router.RouterConstants.PATH_UPDATE;


/**
 * Created by AnyOne on 2018/4/13.
 */


@Route(path = PATH_UPDATE)
public class UpdateDialogActivity extends BaseActivity implements View.OnClickListener {



    private Version version;
    private static final int REQUEST_WRITE_STORAGE = 111;


    @Override
    protected void initUI() {
        version = getIntent().getParcelableExtra(LibConstant.VERSION);
        setListener();
    }


    protected void setListener() {
        findViewById(R.id.tv_update).setOnClickListener(this);
        findViewById(R.id.iv_close).setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_update_dialog;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_update) {
            //请求存储权限
            boolean hasPermission = (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
            if (!hasPermission) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
                ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE);
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    if (AppUtils.isNotificationEnabled(mContext)) {
                        startDownload();
                        finish();
                    } else {
                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                        builder.setMessage("请先打开app通知权限")
                                .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent localIntent = new Intent();
                                        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                        localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                                        startActivity(localIntent);
                                        dialog.dismiss();
                                    }
                                })
                                .setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });

                        builder.create();
                        builder.show();
                    }
                }

            }
        } else if (v.getId() == R.id.iv_close || v.getId() == R.id.tv_cancel) {
            finish();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_WRITE_STORAGE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //获取到存储权限,进行下载
                    startDownload();
                } else {
                    Toast.makeText(this, "不授予存储权限将无法进行下载!", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    /**
     * 启动下载
     */
    private void startDownload() {
        Intent it = new Intent(this, UpdateService.class);
        //下载地址
        it.putExtra("apkUrl", version.getDownloadurl());
         it.putExtra("md5", version.getMd5());
        startService(it);

    }


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }
}
